<?php
require_once('includes/common.php');
###########UPDATE STATUS###########
if (isset($_GET['status'])){
  $id = isset($_GET['id'])?$_GET['id'] :0;

  $status = ($_GET['status']==0)?1:0;

   $updateStatusQuery = "UPDATE `users` SET `status`='$status' WHERE `id`=$id ";
  mysql_query($updateStatusQuery);
  if(mysql_affected_rows()==1){
    echo "Status Updated Successfully";
  }
}

###########UPDATE STATUS F(x)###########
###########DELETE F(x)###########
if (isset($_GET['mode']) && $_GET['mode']=='delete'){
  $id = isset($_GET['id'])?$_GET['id'] :0;
  $deleteQuery = "DELETE FROM `users` WHERE `id`=$id";
  mysql_query($deleteQuery);
  if(mysql_affected_rows()==1){
    echo "Record Deleted Successfully";
  }
}

###########DELETE F(x)###########

#############  SORT STAART    #################
$field = isset($_GET['field'])?$_GET['field'] :'name';
$order = isset($_GET['order'])?$_GET['order'] :'asc';
$orderBy = " ORDER BY $field $order ";
//pr($orderBy);
#############  SORT END   #################

#############  SEARCH STAART    #################
if (isset($_POST['searchText']) ){
  $searchText = $_POST['searchText'];
  $where = " WHERE `name` LIKE '%$searchText%' OR `email` LIKE '%$searchText%' ";
} else {
  $where = '';
}
#############  SEARCH END   #################
/*
 * Paging Params
 * $index or $pageNum INDEX POSITION OF DB RECORDS/ FROM URL
 * $recordPerPage = 5 ;
 * $totalRecords = COUNT FETCH FROM DB
 * $totalPages = ceil($totalRecords/$recordPerPage)
 * $limit = ($pageNum-1) * $recordPerPage;
 * */
$recordPerPage = 2;
$pageNum = isset($_GET['page'])?$_GET['page']:1;
$totalUsers = getTotalRecords($where);
$totalPages = ceil($totalUsers/$recordPerPage);
$limit = ($pageNum-1) * $recordPerPage;
//////////////////////////////////////////////////
$selectQuery = "SELECT * FROM `users` $where $orderBy LIMIT $limit,$recordPerPage";

$selectResult = mysql_query($selectQuery)or die(mysql_error());

$totalUsersonThisPage = mysql_num_rows($selectResult);
?>
<?php
  ////////////////////////////////////
  require_once('includes/header.php');
  ////////////////////////////////////
?>
<form class="form-search" method="post" action="">
  <input type="text" name="searchText" placeholder="Name or Email" style="height:30px;" class="input-large search-query">
  <input type="submit"  class="btn btn-success" value="Please Search">
</form>

Total Users : <?=$totalUsers?><br>

Total Users On This Page : <?=$totalUsersonThisPage?>
<table border="1" class="table table-hover">
  <thead>
    <tr>
      <th>Id</th>
      <th>Name
        <a href="<?=FILE_NAME?>?field=name&order=asc"><i class=" icon-arrow-up"></i></a>
        <a href="<?=FILE_NAME?>?field=name&order=desc"><i class=" icon-arrow-down"></i></a>
      </th>
      <th>Email</th>
      <th>Avatar</th>
      <th>Added Date</th>
      <th>Status</th>
      <th>Options</th>
    </tr>
  </thead>
  <tbody>
<?php
   while($ans = mysql_fetch_assoc($selectResult)) {
?>
    <tr>
      <td>
<a target="_blank" href="my_profile.php?id=<?=$ans['id']?>"><?=$ans['id']?></a>


</td>
      <td>
        <?=$ans['name']?>
      </td>
      <td><a href="mailto:<?=$ans['email']?>"><?=$ans['email']?></a></td>
      <td><img src="uploads/<?=$ans['avatar']?>" height="90" width="120" /></td>
      <td><?=date('d-M-Y H:i:s a l',strtotime($ans['added_date']))?></td>
      <td>
<?php
  echo $status = ($ans['status']==1)?'Active':'Inactive';
  $class = ($ans['status']==1)?'icon-remove':'icon-ok';
?>
<a href="<?=FILE_NAME?>?status=<?=$ans['status']?>&id=<?=$ans['id']?>"><i class="<?=$class?>"></i></a>


      </td>
      <td>

<a onClick="return confirm('Are you sure ?');" href="<?=FILE_NAME?>?mode=delete&id=<?=$ans['id']?>"><i class="icon-trash"></i></a>

<a href="edit_profile.php?id=<?=$ans['id']?>" target="_blank" ><i class="icon-edit"></i></a>

      </td>
    </tr>
<?php }?>
  </tbody>
</table>

<div class="pagination pagination-large pagination-centered">
  <ul>
    <?php
  if($pageNum > '1'){
  ?>
    <li><a href="<?=FILE_NAME?>?page=<?=($pageNum-1)?>">Prev</a></li>
<?php 
}?>
<?php
for($i=1 ; $i<=$totalPages ; $i++) {

  /*
  if($pageNum == $i){
    $class = "active";
  } else {
    $class = "";
  }
  */
  $class = ($pageNum == $i) ? "active":'';
?>
    <li class="<?=$class?>"><a href="<?=FILE_NAME?>?page=<?=$i?>"><?=$i?></a></li>
<?php
}
?>
<?php
if($pageNum<$totalPages){
?>
    <li><a href="<?=FILE_NAME?>?page=<?=($pageNum+1)?>">Next</a></li>
<?php
}
?>
  </ul>
</div>
<?php
function getTotalRecords($where='')
{
  #START Query to get total records
  $totalUsersQuery = "SELECT COUNT(*) AS `total` FROM `users` $where";
  $totalUsersResult = mysql_query($totalUsersQuery) or die(mysql_error());
  $totalUsersAns = mysql_fetch_assoc($totalUsersResult);
  $totalUsers = $totalUsersAns['total'];
  #END Query to get total records
  return $totalUsers;
}
